package wdmail

import (
	"gitee.com/yutiandou/wdtools/wdmail/gomail-v2"
)

type email struct {
	host    string
	port    int
	user    string
	passwd  string
	message *gomail.Message
	dialer  *gomail.Dialer
}

func NewEMailMessage(host string, port int, user, passwd string) I_EMail {
	return &email{
		host:    host,
		port:    port,
		user:    user,
		passwd:  passwd,
		message: gomail.NewMessage(),
	}
}

//SetSrcAddress ..设置邮件发送者地址，name为自定义邮箱别名，无则为“”
func (e *email) SetSrcAddress(address, name string) {
	e.message.SetAddressHeader("From", address, name)
}

//SetdesAddress ..设置收件人地址，可以有很多个
func (e *email) SetdesAddress(des ...string) {
	e.message.SetHeader("To", des...)
}

//SetCc ..设置抄送人
func (e *email) SetCc(cc ...string) {
	e.message.SetHeader("Cc", cc...)
}

//SetSubject ..设置邮件主题
func (e *email) SetSubject(subject string) {
	e.message.SetHeader("subject", subject)
}

//AddAttch ..追加附件
func (e *email) AddAttch(filepath string) {
	e.message.Attach(filepath)
}

//SetBody ..设置正文
func (e *email) SetBody(context string) {
	e.message.SetBody("text/html", context)
}

//SetHeaders ..一个函数全部设置
//参考链接：https://godoc.org/gopkg.in/gomail.v2
func (e *email) SetHeaders(h map[string][]string) {
	e.message.SetHeaders(h)
}

func (e *email) Send() error {
	e.dialer = gomail.NewDialer(e.host, e.port, e.user, e.passwd)
	return e.dialer.DialAndSend(e.message)
}
